package ui.demo;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class OrientateCube extends Activity implements SensorEventListener {

	private GLSurfaceView surface;
	private GLRenderer renderer;
	
	private static boolean fullscreen;
	
	private SensorManager sm;
	private Sensor orientation;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if (fullscreen) {
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
		
		sm = (SensorManager)getSystemService(SENSOR_SERVICE);
    	orientation = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
		
		surface = new GLSurfaceView(this);
		renderer = new GLRenderer();
		surface.setRenderer(renderer);
		setContentView(surface);
		
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		surface.onResume();
        sm.registerListener(this, orientation, SensorManager.SENSOR_DELAY_FASTEST);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		surface.onPause();
    	sm.unregisterListener(this);
	}
	
	@Override
	public void onStop() {
    	sm.unregisterListener(this);
    	super.onStop();
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		//Log.d("OrientationSensorValues"," x: "+event.values[0]+" y: "+event.values[1]+" z: "+event.values[2]);
		Vec3 r = new Vec3(event.values[2]*2,event.values[1]*2,event.values[0]*2);
		try {
			renderer.cube.setRotation(r);
		} catch (Exception e) {}
			
		
	}

	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		
	}
	
}
